//
//  DABookDelayView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/1.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABookDelayView.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DALabel.h"

@interface DABookDelayView ()
@property (nonatomic, strong) DALabel *titleLabel;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) UIButton *delayButton;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) DALabel *privacyLabel;
@property (nonatomic, strong) UISwitch *privacySwitch;
@property (nonatomic, copy) NSString *privacy;
@property (nonatomic, copy) NSString *timeDuration;
@property (nonatomic, strong) UIView *closeImageBackView;


@end

@implementation DABookDelayView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // TODO 默认的设置
        self.layer.cornerRadius = 5;
        self.backgroundColor = [UIColor whiteColor];
        self.buttonArray = [NSMutableArray array];
        
        NSString *delayStr = NSLocalizedString(@"PleaseChooseTheTimeYouWantToDelay", nil);
        
        self.titleLabel = [[DALabel alloc]initWithFrame:CGRectMake((self.frame.size.width - 200) / 2, 30, 200, 20)];
        [self.titleLabel setText:delayStr fitToContent:YES];
        self.titleLabel.textColor = [UIColor colorWithHexString:userNameColor];
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
        
        self.closeImageBackView = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width - 40, 0, 40, 30)];
        self.closeImageBackView.backgroundColor = [UIColor whiteColor];
        self.closeImageBackView.layer.cornerRadius = 5;
        [self addSubview:self.closeImageBackView];
        
        self.cancelImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.closeImageBackView.frame.size.width - 25, 5, 20, 20)];
        self.cancelImage.image = [UIImage imageNamed:@"close"];
        self.cancelImage.userInteractionEnabled = YES;
        [self.closeImageBackView addSubview:self.cancelImage];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCancelImage:)];
        [self.closeImageBackView addGestureRecognizer:tap];
        
        NSString *oneWeek = NSLocalizedString(@"OneWeek", nil);
        NSString *twoWeek = NSLocalizedString(@"twoWeek", nil);
        NSString *threeWeek = NSLocalizedString(@"threeWeek", nil);
        NSString *oneMonth = NSLocalizedString(@"OneMonth", nil);
        NSString *twoMonth = NSLocalizedString(@"twoMonth", nil);
        NSString *threeMonth = NSLocalizedString(@"threeMonth", nil);
        
        /** 循环创建时间按钮*/
        self.titleArray = @[oneWeek, twoWeek, threeWeek, oneMonth, twoMonth, threeMonth];
        for (int i = 0; i < 6; i++) {
            [self creatTimeButtonWithTitle:self.titleArray[i] index:i];
        }
        
        /** 是否设成隐私label*/
      //  UIButton *button = self.buttonArray[3];
        /** 借阅按钮*/
        self.delayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.delayButton.frame = CGRectMake(20, self.frame.size.height - 60, self.frame.size.width - 40, 40);
        NSString *delayButtonStr = NSLocalizedString(@"delayToBorrow", nil);
        self.delayButton.backgroundColor = [UIColor supportGreenColor];
        self.delayButton.layer.cornerRadius = 5;
        [self.delayButton setTitle:delayButtonStr forState:UIControlStateNormal];
        [self.delayButton addTarget:self action:@selector(clickDelayButton:) forControlEvents:UIControlEventTouchUpInside];
        self.delayButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.delayButton];

    }
    return self;
}

#pragma mark - 创建时间按钮
- (void)creatTimeButtonWithTitle:(NSString *)title index:(int)index{
    CGFloat buttonWidth = (self.frame.size.width - 60)/ 3;
    CGFloat buttonHeight = 40;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor colorWithHexString:backGroundColor];
    [button setTitle:title forState:UIControlStateNormal];
    button.frame = CGRectMake(20 + (buttonWidth + 10) * (index % 3), self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 30 + (buttonHeight + 20) * (index / 3), buttonWidth, buttonHeight);
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitleColor:[UIColor colorWithHexString:lostButtonColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chooseBorrowTimeButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = index + 2000;
    button.layer.cornerRadius = 5;
    [self addSubview:button];
    [self.buttonArray addObject:button];
}
#pragma mark - 时间按钮点击方法
- (void)chooseBorrowTimeButton:(UIButton *)btn{
    for (int i = 0 ; i < 6; i++) {
        UIButton *button  = (UIButton *)[self viewWithTag:i + 2000];
        button.layer.borderWidth = 0;
    }
    UIButton *button = (UIButton *)[self viewWithTag:btn.tag];
    button.layer.borderColor = [UIColor supportRedColor].CGColor;
    button.layer.borderWidth = 1;
    button.clipsToBounds = YES;
    
    NSString *oneWeek = NSLocalizedString(@"OneWeek", nil);
    NSString *twoWeek = NSLocalizedString(@"twoWeek", nil);
    NSString *threeWeek = NSLocalizedString(@"threeWeek", nil);
    NSString *oneMonth = NSLocalizedString(@"OneMonth", nil);
    NSString *twoMonth = NSLocalizedString(@"twoMonth", nil);
    NSString *threeMonth = NSLocalizedString(@"threeMonth", nil);
    
    if ([button.titleLabel.text isEqualToString:oneWeek]) {
        self.timeDuration = @"1w";
    }
    if ([button.titleLabel.text  isEqualToString:twoWeek]) {
        self.timeDuration = @"2w";
    }
    if ([button.titleLabel.text isEqualToString:threeWeek]) {
        self.timeDuration = @"3w";
    } if ([button.titleLabel.text isEqualToString:oneMonth]) {
        self.timeDuration = @"1m";
    }if ([button.titleLabel.text isEqualToString:twoMonth]) {
        self.timeDuration = @"2m";
    }if ([button.titleLabel.text  isEqualToString:threeMonth]) {
        self.timeDuration = @"3m";
    }
    
    NSLog(@"%@", self.timeDuration);
}

#pragma mark - 延期按钮点击方法
- (void)clickDelayButton:(UIButton *)btn{
    NSLog(@"点击了延期借阅");
    [self.delegate borrowExtend:self.timeDuration];
    
}
#pragma mark - cancel Image
- (void)clickCancelImage:(UITapGestureRecognizer *)tap{
    // 点击close image 图片
    [self.delegate clickBookDelayCloseImage];
    NSLog(@"延期view上的关闭按钮");
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
